import friendfeed
import friendfeedusers
import sys
import urllib2

username = sys.argv[1]

def getuser(username):
	service = friendfeed.FriendFeed()
	user = friendfeedusers.User()
	profile = service._fetch_feed('/api/user/%s/profile'%username)
	
	user.ID = profile['id']
	user.Name = profile['name']
	user.ProfileURL = profile['profileUrl']
	user.NickName = profile['nickname']

	for sub in profile['subscriptions']:
		s = friendfeedusers.Subscription()
		s.ID = sub['id']
		s.Name = sub['name']
		s.NickName = sub['nickname']
		s.URL = sub['profileUrl']
		user.Subscriptions.append(s)
	
	for room in profile['rooms']:
		r = friendfeedusers.Room()
		r.ID = room['id']
		r.Name = room['name']
		r.NickName = room['nickname']
		r.URL = room['url']
		user.Rooms.append(r)
	
	for service in profile['rooms']:
		s = friendfeedusers.Room()
		s.ID = service['id']
		s.Name = service['name']
		s.NickName = service['nickname']
		s.URL = service['url']
		user.Services.append(r)
	return user

if __name__=='__main__':
	ffu = friendfeedusers.FriendFeedUsers(username + '.ffusers')
	user = getuser(username)
	count = 0
	for s in user.Subscriptions:
		try:
			ffu.persist(getuser(s.NickName))
			count += 1
			print 'Done ' + str(count) + ' of ' +  str(len(user.Subscriptions))
		except urllib2.HTTPError:			
			print "Private Feed. Skipping"



